package com.smartlifev30.baiwei.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;

import com.smartlifev30.baiwei.view.dialog.NotifyTipsDialog;
import com.smartlifev30.baiwei.view.dialog.ProgressDialog;
import com.smartlifev30.mvp.presenter.BasePresenter;

import smartlifev30.com.sdk.net.baiwei.NetManager;
import smartlifev30.com.sdk.net.core.ConnectStatus;
import smartlifev30.com.sdk.net.core.IClient;

/**
 * @author tys
 * @date 2018/8/14
 */

public abstract class BaseActivity<P extends BasePresenter> extends AppCompatActivity {
    protected P presenter;
    protected ProgressDialog mProgressDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        presenter = createPresent();
    }

    @Override
    protected void onResume() {
        super.onResume();

        {
            IClient<String, String> client = NetManager.getInstance().getClient();
            ConnectStatus connectStatus = client.getConnectStatus();
            if (connectStatus != ConnectStatus.CONNECT_SUCCEED) {
                NotifyTipsDialog dialog = new NotifyTipsDialog.Builder(this)
                        .them("提示")
                        .content("断开连接")
                        .build();
                dialog.setContent("断开连接!");
                dialog.show();

                NetManager.getInstance().connect("114.215.82.238", 17030, new IClient.ConnectListen() {
                    @Override
                    public void onSuccess() {
                        Log.e("0-0- tys", "连接成功");
                        dialog.dismiss();
                    }

                    @Override
                    public void onFailed(Throwable e) {
                        Log.e("0-0- tys", "连接失败:" + e.toString());
                    }
                });
            }
        }


    }

    protected abstract P createPresent();

    @Override
    protected void onDestroy() {
        if (presenter != null) {
            presenter.detach();
        }
        super.onDestroy();
    }

    public void toast(String context) {
        Toast.makeText(this, context, Toast.LENGTH_SHORT).show();
    }

    public void toast(@StringRes int resId) {
        Toast.makeText(this, getString(resId), Toast.LENGTH_SHORT).show();
    }

    public void showLoading() {
        if (mProgressDialog == null) {
            mProgressDialog = new ProgressDialog(this);
        } else {
            if (mProgressDialog.isShowing()) {
                mProgressDialog.dismiss();
            }
        }
        mProgressDialog.show();
    }

    public void dismissLoading() {
        if (mProgressDialog != null && mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
        }
    }

}
